#import "drawundo.h"

@interface UngroupGraphicsChange(PrivateMethods)

@end

@implementation UngroupGraphicsChange

- initGraphicView:aGraphicView
{
    [super init];
    graphicView = aGraphicView;
    changeDetails = nil;
    groups = nil;

    return self;
}

- free
{
    int i, count;
    id group;

    if ([self hasBeenDone]) {
	count = [groups count];
	for (i = 0; i < count; i++) {
	    group = [groups objectAt:i];
	    [[group subGraphics] empty];
	    [group free];
	}
    }
    [groups free];
    if (changeDetails != nil) {
	[changeDetails freeObjects];
	[changeDetails free];
    }
    return [super free];
}

- (const char *)changeName
{
    return NXLocalStringFromTable("Operations", "Ungroup", NULL, "The operation of ungroup a bunch of graphical entities that are grouped together into a single graphical entity.");
}

- saveBeforeChange
{
    List *selectedGraphics;
    int i, count;
    id g;
    id changeDetailClass;

    groups = [[List alloc] init];
    changeDetailClass = [self changeDetailClass];
    changeDetails = [[List alloc] init];

    selectedGraphics = [graphicView selectedGraphics];
    count = [selectedGraphics count];
    for (i = 0; i < count; i++) {
	g = [selectedGraphics objectAt:i];
	if ([g isKindOf:[Group class]]) {
	    [groups addObject:g];
	    [changeDetails addObject:[[changeDetailClass alloc] initGraphic:g change:self]];
	}
    }
    [changeDetails makeObjectsPerform:@selector(recordGraphicPositionIn:) with:[graphicView graphics]];

    count = [groups count];
    if (count == 0)
        [self disable];
	
    return self;
}

- undoChange
{
    List *allGraphics, *graphics;
    int i, j, count, graphicCount;
    NXRect affectedBounds;
    id group, graphic, detail;

    allGraphics = [graphicView graphics];
    count = [changeDetails count];
    for (i = 0; i < count; i++) {
        detail = [changeDetails objectAt:i];
	group = [detail graphic];
	graphics = [group subGraphics];
	graphicCount = [graphics count];
	for (j = 0; j < graphicCount; j++) {
	    graphic = [graphics objectAt:j];
	    [graphic setCacheable:NO];
	    [allGraphics removeObject:graphic];
	}
	[allGraphics insertObject:group at:[detail graphicPosition]];
    }
    
    [graphicView getSelection];
    [graphicView setGroupInSlist:YES];
    [graphicView getBBox:&affectedBounds of:groups];
    [graphicView cache:&affectedBounds];
    [[graphicView window] flushWindow];
    [[[NXApp inspectorPanel] delegate] loadGraphic:[graphicView selectedGraphic]]; 

    return [super undoChange];
}

- redoChange
{
    List *allGraphics;
    int k;
    int i, count;
    NXRect affectedBounds;
    id group;

    [graphicView getBBox:&affectedBounds of:groups];

    allGraphics = [graphicView graphics];
    count = [groups count];
    for (i = 0; i < count; i++) {
        group = [groups objectAt:i];
	k = [allGraphics indexOf:group];
	[allGraphics removeObjectAt:k];
	[group transferSubGraphicsTo:allGraphics at:k];
    }

    [graphicView getSelection];
    [graphicView resetGroupInSlist];
    [graphicView cache:&affectedBounds];
    [[graphicView window] flushWindow];
    [[[NXApp inspectorPanel] delegate] loadGraphic:[graphicView selectedGraphic]]; 


    return [super redoChange];
}

- changeDetailClass
{
    return [OrderChangeDetail class];
}

@end